package services;

import domain.entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 06/10/12
 * Time: 19:51
 * To change this template use File | Settings | File Templates.
 */
@Service
public class SiteLevelDocumentsService {

    private ProtocoloCentroDAO protocoloCentroDAO;
    private ProtocolinformationDAO protocolinformationDAO;
    private InformedconsentsDAO informedconsentsDAO;
    private PrintedmaterialforpatientDAO printedmaterialforpatientDAO;
    private DatasafetyreportsDAO datasafetyreportsDAO;
    private ReaprobacionesDAO reaprobacionesDAO;
    private AdvancedfinalstudyreportsDAO advancedfinalstudyreportsDAO;
    private SecdiscrepancyreportDAO secdiscrepancyreportDAO;
    private StudystaffDAO studystaffDAO;
    private StudydeviationsDAO studydeviationsDAO;
    private SuplementalcrfcspDAO suplementalcrfcspDAO;
    private SaesDAO saesDAO;
    private CtagDAO ctagDAO;
    private StudystartupDAO studystartupDAO;
    private ClosuredocumentsDAO closuredocumentsDAO;

    public SiteLevelDocumentsService() {
    }

    @Autowired
    public SiteLevelDocumentsService(ProtocolinformationDAO protocolinformationDAO, InformedconsentsDAO informedconsentsDAO, ProtocoloCentroDAO protocoloCentroDAO, PrintedmaterialforpatientDAO printedmaterialforpatientDAO, DatasafetyreportsDAO datasafetyreportsDAO, ReaprobacionesDAO reaprobacionesDAO, AdvancedfinalstudyreportsDAO advancedfinalstudyreportsDAO, SecdiscrepancyreportDAO secdiscrepancyreportDAO, StudystaffDAO studystaffDAO, StudydeviationsDAO studydeviationsDAO, SuplementalcrfcspDAO suplementalcrfcspDAO, SaesDAO saesDAO, CtagDAO ctagDAO, StudystartupDAO studystartupDAO, ClosuredocumentsDAO closuredocumentsDAO) {
        this.protocolinformationDAO = protocolinformationDAO;
        this.informedconsentsDAO = informedconsentsDAO;
        this.protocoloCentroDAO = protocoloCentroDAO;
        this.printedmaterialforpatientDAO = printedmaterialforpatientDAO;
        this.datasafetyreportsDAO = datasafetyreportsDAO;
        this.reaprobacionesDAO = reaprobacionesDAO;
        this.advancedfinalstudyreportsDAO = advancedfinalstudyreportsDAO;
        this.secdiscrepancyreportDAO = secdiscrepancyreportDAO;
        this.studystaffDAO = studystaffDAO;
        this.studydeviationsDAO = studydeviationsDAO;
        this.suplementalcrfcspDAO = suplementalcrfcspDAO;
        this.saesDAO = saesDAO;
        this.ctagDAO = ctagDAO;
        this.studystartupDAO = studystartupDAO;
        this.closuredocumentsDAO = closuredocumentsDAO;
    }

    //PROTOCOL INFORMATION
    public List<Protocolinformation> findProtocolInformationBySiteId(Integer siteId){
        return protocolinformationDAO.findBySiteId(siteId);
    }

    public void saveProtocolInformation(Protocolinformation protocolinformation, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais){
        if(protocolinformation.getScope() == null || protocolinformation.getScope().intValue() == 1){
            protocolinformation.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            protocolinformationDAO.attachDirty(protocolinformation);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                protocolinformation.setProtocoloCentro(pc);
                protocolinformationDAO.attachDirty(protocolinformation);
            }
        }
    }

    public Protocolinformation getProtocolInformationById(Integer id) {
        return protocolinformationDAO.findById(id);
    }


    //INFORMED CONSENTS
    public List<Informedconsents> findInformedConsentsBySiteId(Integer siteId) {
        return informedconsentsDAO.findBySiteId(siteId);
    }

    public void saveInformedConsents(Informedconsents informedconsents, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais){
        if(informedconsents.getScope() == null || informedconsents.getScope().intValue() == 1){
            informedconsents.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            informedconsentsDAO.attachDirty(informedconsents);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                informedconsents.setProtocoloCentro(pc);
                informedconsentsDAO.attachDirty(informedconsents);
            }
        }
    }

    public Informedconsents getInformedConsentById(Integer id) {
        return informedconsentsDAO.findById(id);
    }

    //PRINTED MATERIAL FOR PATIENTS
    public List<Printedmaterialforpatient> findPrintedMaterialsBySiteId(Integer siteId) {
        return printedmaterialforpatientDAO.findBySiteId(siteId);
    }

    public void savePrintedMaterials(Printedmaterialforpatient printedMaterials, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(printedMaterials.getScope() == null || printedMaterials.getScope().intValue() == 1){
            printedMaterials.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            printedmaterialforpatientDAO.attachDirty(printedMaterials);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                printedMaterials.setProtocoloCentro(pc);
                printedmaterialforpatientDAO.attachDirty(printedMaterials);
            }
        }
    }

    public Printedmaterialforpatient getPrintedMaterialById(Integer siteLevelDocumentId) {
        return printedmaterialforpatientDAO.findById(siteLevelDocumentId);
    }

    //DATA SAFETY REPORTS
    public List<Datasafetyreports> findDataSafetyReportsBySiteId(Integer siteId) {
        return datasafetyreportsDAO.findBySiteId(siteId);
    }

    public void saveDataSafety(Datasafetyreports dataSafety, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(dataSafety.getScope() == null || dataSafety.getScope().intValue() == 1){
            dataSafety.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            datasafetyreportsDAO.attachDirty(dataSafety);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                dataSafety.setProtocoloCentro(pc);
                datasafetyreportsDAO.attachDirty(dataSafety);
            }
        }
    }

    public Datasafetyreports getDataSafetyById(Integer siteLevelDocumentId) {
        return datasafetyreportsDAO.findById(siteLevelDocumentId);
    }

    //REAPROBACIONES
    public List<Reaprobaciones> findReaprobacionesBySiteId(Integer siteId) {
        return reaprobacionesDAO.findBySiteId(siteId);
    }

    public void saveReaprobaciones(Reaprobaciones reaprobaciones, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(reaprobaciones.getScope() == null || reaprobaciones.getScope().intValue() == 1){
            reaprobaciones.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            reaprobacionesDAO.attachDirty(reaprobaciones);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                reaprobaciones.setProtocoloCentro(pc);
                reaprobacionesDAO.attachDirty(reaprobaciones);
            }
        }
    }

    public Reaprobaciones getReaprobacionesById(Integer siteLevelDocumentId) {
        return reaprobacionesDAO.findById(siteLevelDocumentId);
    }

    //ADVANCED FINAL STUDY REPORTS
    public List<Advancedfinalstudyreports> findAdvancedfinalstudyreportsBySiteId(Integer siteId) {
        return advancedfinalstudyreportsDAO.findBySiteId(siteId);
    }

    public void saveAdvancedfinalstudyreports(Advancedfinalstudyreports advancedfinalstudyreports, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(advancedfinalstudyreports.getScope() == null || advancedfinalstudyreports.getScope().intValue() == 1){
            advancedfinalstudyreports.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            advancedfinalstudyreportsDAO.attachDirty(advancedfinalstudyreports);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                advancedfinalstudyreports.setProtocoloCentro(pc);
                advancedfinalstudyreportsDAO.attachDirty(advancedfinalstudyreports);
            }
        }
    }

    public Advancedfinalstudyreports getAdvancedfinalstudyreportsById(Integer siteLevelDocumentId) {
        return advancedfinalstudyreportsDAO.findById(siteLevelDocumentId);
    }

    //SEC DISCREPANCY REPORT
    public List<Secdiscrepancyreport> findSecdiscrepancyreportBySiteId(Integer siteId) {
        return secdiscrepancyreportDAO.findBySiteId(siteId);
    }

    public void saveSecdiscrepancyreport(Secdiscrepancyreport secdiscrepancyreport, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(secdiscrepancyreport.getScope() == null || secdiscrepancyreport.getScope().intValue() == 1){
            secdiscrepancyreport.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            secdiscrepancyreportDAO.attachDirty(secdiscrepancyreport);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                secdiscrepancyreport.setProtocoloCentro(pc);
                secdiscrepancyreportDAO.attachDirty(secdiscrepancyreport);
            }
        }
    }

    public Secdiscrepancyreport getSecdiscrepancyreportById(Integer siteLevelDocumentId) {
        return secdiscrepancyreportDAO.findById(siteLevelDocumentId);
    }

    //STUDY STAFF
    public List<Studystaff> findStudyStaffBySiteId(Integer siteId) {
        return studystaffDAO.findBySiteId(siteId);
    }

    public void saveStudystaff(Studystaff studystaff, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(studystaff.getScope() == null || studystaff.getScope().intValue() == 1){
            studystaff.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            studystaffDAO.attachDirty(studystaff);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                studystaff.setProtocoloCentro(pc);
                studystaffDAO.attachDirty(studystaff);
            }
        }
    }

    public Studystaff getStudystaffById(Integer siteLevelDocumentId) {
        return studystaffDAO.findById(siteLevelDocumentId);
    }

    //ADVANCED FINAL STUDY REPORTS
    public List<Studydeviations> findStudydeviationsBySiteId(Integer siteId) {
        return studydeviationsDAO.findBySiteId(siteId);
    }

    public void saveStudydeviations(Studydeviations studydeviations, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(studydeviations.getScope() == null || studydeviations.getScope().intValue() == 1){
            studydeviations.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            studydeviationsDAO.attachDirty(studydeviations);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                studydeviations.setProtocoloCentro(pc);
                studydeviationsDAO.attachDirty(studydeviations);
            }
        }
    }

    public Studydeviations getStudydeviationsById(Integer siteLevelDocumentId) {
        return studydeviationsDAO.findById(siteLevelDocumentId);
    }

    //SUPLEMENTAL CRF/CSP
    public List<Suplementalcrfcsp> findSuplementalcrfcspBySiteId(Integer siteId) {
        return suplementalcrfcspDAO.findBySiteId(siteId);
    }

    public void saveSuplementalcrfcsp(Suplementalcrfcsp suplementalcrfcsp, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(suplementalcrfcsp.getScope() == null || suplementalcrfcsp.getScope().intValue() == 1){
            suplementalcrfcsp.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            suplementalcrfcspDAO.attachDirty(suplementalcrfcsp);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                suplementalcrfcsp.setProtocoloCentro(pc);
                suplementalcrfcspDAO.attachDirty(suplementalcrfcsp);
            }
        }
    }

    public Suplementalcrfcsp getSuplementalcrfcspById(Integer siteLevelDocumentId) {
        return suplementalcrfcspDAO.findById(siteLevelDocumentId);
    }

    //SAEs
    public List<Saes> findSaesBySiteId(Integer siteId) {
        return saesDAO.findBySiteId(siteId);
    }

    public void saveSaes(Saes saes, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(saes.getScope() == null || saes.getScope().intValue() == 1){
            saes.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            saesDAO.attachDirty(saes);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                saes.setProtocoloCentro(pc);
                saesDAO.attachDirty(saes);
            }
        }
    }

    public Saes getSaesById(Integer siteLevelDocumentId) {
        return saesDAO.findById(siteLevelDocumentId);
    }

    //CTAg
    public List<Ctag> findCtagBySiteId(Integer siteId) {
        return ctagDAO.findBySiteId(siteId);
    }

    public void saveCtag(Ctag ctag, Integer siteId, ProtocoloCompromisopais protocoloCompromisopais) {
        if(ctag.getScope() == null || ctag.getScope().intValue() == 1){
            ctag.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, protocoloCompromisopais.getId().getProtocoloid(), protocoloCompromisopais.getId().getCompromisopaisid())));
            ctagDAO.attachDirty(ctag);
        }
        else{
            for(ProtocoloCentro pc : protocoloCentroDAO.findByProtocoloCompromisopais(protocoloCompromisopais)){
                ctag.setProtocoloCentro(pc);
                ctagDAO.attachDirty(ctag);
            }
        }
    }

    public Ctag getCtagById(Integer siteLevelDocumentId) {
        return ctagDAO.findById(siteLevelDocumentId);
    }

    //STUDY STARTUP
    public ArrayList<Studystartup> findStudyStartupBySiteId(Integer siteId) {
        return studystartupDAO.findBySiteId(siteId);
    }

    public void saveStudystartup(ProtocoloCompromisopais pcp, ArrayList<Studystartup> list, Integer siteId) {
        for(Studystartup s : list){
            s.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, pcp.getId().getProtocoloid(), pcp.getId().getCompromisopaisid())));
            studystartupDAO.attachDirty(s);
        }
    }

    //CLOSURE DOCUMENTS
    public ArrayList<Closuredocuments> findClosureDocumentBySiteId(Integer siteId) {
        return closuredocumentsDAO.findBySiteId(siteId);
    }

    public void saveClosuredocuments(ProtocoloCompromisopais pcp, ArrayList<Closuredocuments> list, Integer siteId) {
        for(Closuredocuments c : list){
            c.setProtocoloCentro(protocoloCentroDAO.findById(new ProtocoloCentroId(siteId, pcp.getId().getProtocoloid(), pcp.getId().getCompromisopaisid())));
            closuredocumentsDAO.attachDirty(c);
        }
    }
}
